package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.model.dto.isolation.room.PersonDto;
import com.lnking.lnkingplace.model.dto.isolation.room.RoomExcelDto;
import com.lnking.lnkingplace.model.dto.isolation.room.RoomListDto;
import com.lnking.lnkingplace.model.request.isolation.room.RoomAddRequest;
import com.lnking.lnkingplace.model.request.isolation.room.RoomSearchRequest;
import com.lnking.lnkingplace.model.request.isolation.room.RoomUpdateRequest;
import com.lnking.lnkingplace.model.request.isolation.room.RoomUpdateStatusRequest;
import com.lnking.lnkingplace.model.request.isolation.roomSweep.RoomSweepAddRequest;
import com.lnking.lnkingplace.model.request.isolation.roomSweep.RoomSweepUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationRoomService;
import com.lnking.lnkingplace.service.IRoomSweepRecordService;
import com.lnking.starter.export.utils.ExportUtils;
import com.lnking.starter.mybatis.entity.IPageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-27
 */
@RestController
@RequestMapping("/isolation-room")
@Api(tags = "隔离房间")
public class IsolationRoomController {
    
    @Resource
    IIsolationRoomService isolationRoomService;

    @Resource
    IRoomSweepRecordService roomSweepRecordService;

    @ApiOperation("获取房间状态及信息")
    @GetMapping("get/room/info")
    public ReturnResult getRoomInfo(@RequestParam String placeName,
                                      @RequestParam String roomNumber){
        return ReturnResult.success(isolationRoomService.getRoomInfo(placeName,roomNumber));
    }

    @ApiOperation("查询房间使用情况(个数)")
    @GetMapping("query/room/count")
    public ReturnResult queryRoomCount(String placeId){

        return ReturnResult.success(isolationRoomService.queryRoomCount(placeId));
    }

    @ApiOperation("开始消杀")
    @PostMapping("create/room/sweep")
    public ReturnResult createRoomSweep(@RequestBody RoomSweepAddRequest params){
        return ReturnResult.success(roomSweepRecordService.createRoomSweep(params));
    }

    @ApiOperation("结束消杀")
    @PostMapping("end/room/sweep")
    public ReturnResult endRoomSweep(@RequestBody RoomSweepUpdateRequest params){
        roomSweepRecordService.endRoomSweep(params);
        return ReturnResult.success();
    }

    @ApiOperation("手动同步数据")
    @GetMapping("update/data")
    public ReturnResult updateData(){
        isolationRoomService.updateData();
        return ReturnResult.success();
    }

    @ApiOperation("查询列表")
    @GetMapping("list")
    public ReturnResult getList(RoomSearchRequest params) {
        IPageInfo<RoomListDto> pageInfo = (IPageInfo<RoomListDto>)isolationRoomService.pageList(params);
        List<RoomListDto> list = pageInfo.getList();
        //查询人员信息
        list.forEach(result ->{
            String roomNumber = result.getRoomNumber();
            String placeId = result.getPlaceId();
            PersonDto personDto = isolationRoomService.queryPerson(placeId, roomNumber);
            if(null != personDto){
                result.setPersonDto(personDto);
            }
        });
        pageInfo.setList(list);
        return ReturnResult.success(pageInfo);
    }

    @ApiOperation(value = "导出")
    @GetMapping("export")
    public void export(RoomSearchRequest params, HttpServletResponse response) throws Exception {
        List<RoomListDto> data = (List<RoomListDto>)isolationRoomService.pageList(params);
        ExportUtils.export(data, response, RoomExcelDto.class);
    }

    @ApiOperation("导入")
    @PostMapping("import")
    public ReturnResult importExcel(String placeId, @RequestParam("file") MultipartFile file) throws Exception{
        isolationRoomService.importExcel(placeId,file);
        return ReturnResult.success();
    }

    @PostMapping("add")
    @ApiOperation("新增")
    public ReturnResult add(@Valid @RequestBody RoomAddRequest params) throws Exception{
        isolationRoomService.add(params);
        return ReturnResult.success();
    }

    @PostMapping("update")
    @ApiOperation("修改")
    public ReturnResult update(@Valid @RequestBody RoomUpdateRequest params) throws Exception{
        isolationRoomService.update(params);
        return ReturnResult.success();
    }

    @PostMapping("update-status")
    @ApiOperation("修改状态")
    public ReturnResult updateStatus(@RequestBody List<RoomUpdateStatusRequest> params) throws Exception{
        isolationRoomService.updateStatus(params);
        return ReturnResult.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        isolationRoomService.removeById(id);
        return ReturnResult.success();
    }
}
